import * as monaco from "monaco-editor-core";

class LanguageServiceDefaultsImpl {
  _onDidChange = new monaco.Emitter();
  _diagnosticsOptions;
  _modeConfiguration;
  _languageId;

  constructor(languageId, diagnosticsOptions, modeConfiguration) {
    this._languageId = languageId;
    this.setDiagnosticsOptions(diagnosticsOptions);
    this.setModeConfiguration(modeConfiguration);
  }

  get onDidChange() {
    return this._onDidChange.event;
  }

  get languageId() {
    return this._languageId;
  }

  get modeConfiguration() {
    return this._modeConfiguration;
  }

  get diagnosticsOptions() {
    return this._diagnosticsOptions;
  }

  setDiagnosticsOptions(options) {
    this._diagnosticsOptions = options || Object.create(null);
    this._onDidChange.fire(this);
  }

  setModeConfiguration(modeConfiguration) {
    this._modeConfiguration = modeConfiguration || Object.create(null);
    this._onDidChange.fire(this);
  }
}

const diagnosticDefault = {
  validate: true,
  allowComments: true,
  schemas: [],
  enableSchemaRequest: false,
  schemaRequest: "warning",
  schemaValidation: "warning",
  comments: "error",
  trailingCommas: "error",
};

const modeConfigurationDefault = {
  documentFormattingEdits: true,
	documentRangeFormattingEdits: true,
	completionItems: true,
	hovers: true,
	documentSymbols: true,
	tokens: true,
	colors: true,
	foldingRanges: true,
	diagnostics: true,
	selectionRanges: true
};

const jsonDefaults = new LanguageServiceDefaultsImpl(
  "json",
  diagnosticDefault,
  modeConfigurationDefault
);

monaco.languages.register({
  id: "json",
  extensions: [
    ".json",
    ".bowerrc",
    ".jshintrc",
    ".jscsrc",
    ".eslintrc",
    ".babelrc",
    ".har",
  ],
  aliases: ["JSON", "json"],
  mimetypes: ["application/json"],
});

function getMode() {
  return import("./jsonMode");
}

monaco.languages.onLanguage("json", () => {
  getMode().then((mode) => mode.setupMode(jsonDefaults));
});
